package com.cax.module.article.controller;

import com.cax.common.core.validate.Group;
import com.cax.common.domain.base.dto.IdIntegerDTO;
import com.cax.module.article.domain.dto.ArticlePageDTO;
import com.cax.module.file.domain.FileType;
import com.cax.module.file.domain.vo.File2VO;
import com.cax.module.file.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import com.cax.module.article.service.ArticleService;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.cax.module.article.domain.Article;
import com.cax.module.article.domain.vo.ArticleVO;
import com.cax.module.article.domain.dto.ArticleDTO;
import com.cax.common.core.domain.Result;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import javax.validation.constraints.NotEmpty;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@RestController
@Api(tags = "文章")
@RequestMapping("/article")

public class ArticleController {
    /**
     * 文章表 Service
     */
    @Autowired
    public ArticleService articleService;
	@Autowired
    public FileService fileService;

    @GetMapping("/getPage/noAuth")
    @ApiOperation(value = "分页查询", notes = "")
	public Result<Page<ArticleVO>> getByPage(
		@Validated ArticlePageDTO dto
	) {
		Page<ArticleVO> result = articleService.getPage(dto);
		return Result.ok(result);
	}

    @GetMapping("/getByKey/noAuth")
    @ApiOperation(value = "通过主键查询", notes = "")
    @ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "int"),
	})
	public Result<ArticleVO> getByKey(
			@NotNull(message = "参数id不能为空")Integer id
	) {
		ArticleVO result = articleService.getByKey(id);
		return Result.ok(result);
	}

	@PostMapping("addTitleImg")
	@ApiOperation(value = "上传-标题图片", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"),
	})
	public Result<File2VO> addTitleImg(
			@RequestParam(name = "file") MultipartFile file
	) {
		File2VO result = fileService.upload(FileType.TypeEnum.FILE_TYPE_1.getCode(),file);
		return Result.ok(result);
	}

	@PostMapping("addRichFile")
	@ApiOperation(value = "上传-添加富文本文件", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"),
	})
	public Result<File2VO> addRichFile(
			@RequestParam(name = "file") MultipartFile file
	) {
		File2VO result = fileService.upload(FileType.TypeEnum.FILE_TYPE_2.getCode(),file);
		return Result.ok(result);
	}

	@PostMapping("/add")
    @ApiOperation(value = "添加", notes = "")
	public Result<Integer> add(
		@RequestBody @Validated(Group.Add.class)  ArticleDTO dto
	) {
		Integer result = articleService.add(dto);
		return Result.ok(result);
	}

	@PostMapping("/upd")
    @ApiOperation(value = "通过主键修改", notes = "")
	public Result upd(
		@RequestBody @Validated(Group.Upd.class)  ArticleDTO dto
	) {
		articleService.upd(dto);
		return Result.ok();
	}

	@PostMapping("/del")
	@ApiOperation(value = "删除", notes = "")
	public Result del(
			@RequestBody @Validated IdIntegerDTO dto
	) {
		articleService.del(dto);
		return Result.ok();
	}

}

